<template>
  <div class="index">
    <el-row :gutter="20" v-permission="['getStatistics1,GET']">
      <template v-if="panels.length === 0">
        <el-col
          :span="6"
          :sm="6"
          :xs="12"
          :offset="0"
          v-for="item in 4"
          :key="item"
        >
          <el-card shadow="hover">
            <el-skeleton style="width: 100px" animated loading>
              <template #template>
                <div slot="header" class="card-header">
                  <el-skeleton-item variant="p" style="width: 50%" />
                  <el-skeleton-item variant="p" style="width: 10%" />
                </div>
                <div class="text-3xl font-bold text-gray-500 pt-5">
                  <el-skeleton-item variant="p" style="width: 80%" />
                </div>
                <el-divider />
                <div class="flex justify-between text-sm text-gray-500">
                  <el-skeleton-item variant="p" style="width: 50%" />
                  <el-skeleton-item variant="p" style="width: 10%" />
                </div>
              </template>
            </el-skeleton>
          </el-card>
        </el-col>
      </template>

      <el-col
        v-else
        :span="6"
        :sm="6"
        :xs="12"
        :offset="0"
        v-for="(item, index) in panels"
        :key="index"
      >
        <el-card shadow="hover" class="border-0">
          <div slot="header" class="card-header">
            <span class="text-sm">{{ item.title }}</span>
            <el-tag :type="item.unitColor" effect="plain">
              {{ item.unit }}
            </el-tag>
          </div>
          <div class="text-3xl font-bold text-gray-500 pt-5">
            <CountTo :value="item.value" />
          </div>
          <el-divider />
          <div class="flex justify-between text-sm text-gray-500">
            <span>{{ item.subTitle }}</span>
            <span>{{ item.subValue }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <index-navs />

    <el-row :gutter="20" class="mt-2">
      <el-col :span="12" :offset="0">
        <index-chart v-permission="['getStatistics3,GET']" />
      </el-col>
      <el-col :span="12" :offset="0" v-permission="['getStatistics2,GET']">
        <index-card
          title="店铺即商品提示"
          tip="店铺即商品提示"
          :btns="goods"
        ></index-card>
        <index-card
          class="mt-3"
          title="交易提示"
          tip="需要立即处理的交易订单"
          :btns="order"
        ></index-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
  import { ref } from 'vue'
  import { getStatistics1, getStatistics2 } from '@/api/index.js'
  import CountTo from '@/components/CountTo.vue'
  import IndexNavs from '@/components/IndexNavs.vue'
  import IndexChart from '@/components/IndexChart.vue'
  import IndexCard from '@/components/IndexCard.vue'

  const panels = ref([])
  getStatistics1().then((res) => {
    panels.value = res.panels
  })

  const goods = ref([])
  const order = ref([])
  getStatistics2().then((res) => {
    goods.value = res.goods
    order.value = res.order
  })
</script>

<style scoped>
  .index .card-header {
    @apply flex items-center justify-between;
  }
</style>
